package xin.marcher.module.lifecycle.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.lifecycle.domain.dto.ItemPeriodStatusDTO;
import xin.marcher.module.lifecycle.domain.request.ItemPeriodRequest;
import xin.marcher.module.lifecycle.domain.request.ItemPeriodStatusRequest;
import xin.marcher.module.lifecycle.domain.response.ItemPeriodStageResponse;
import xin.marcher.module.lifecycle.repository.ItemPeriodStageRepository;
import xin.marcher.module.lifecycle.service.ItemPeriodStatusService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 *
 */
@Service
public class ItemPeriodStatusServiceImpl implements ItemPeriodStatusService {

    @Resource
    private ItemPeriodStageRepository itemPeriodStageRepository;

    /**
     * 批量获取商品状态信息
     *
     * @param request 查询参数，itemId、itemName、itemStatus
     * @return 商品信息，包括商品状态、生命周期状态
     */
    @Override
    public PageResult<ItemPeriodStatusDTO> pageItemStatus(ItemPeriodStatusRequest request) {
        // 返回结果信息
        return itemPeriodStageRepository.pageItemStatus(request);
    }


    /**
     * 保存商品的状态
     *
     * @param request itemId、itemStatus
     * @return 是否修改成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemPeriodStageResponse saveItemStatus(ItemPeriodStatusRequest request) {
        // 入参检查
        checkParams(request);
        // 保存商品状态
        itemPeriodStageRepository.saveItemStatus(request);

        // 保存生命周期状态
        ItemPeriodRequest periodRequest = new ItemPeriodRequest();
        periodRequest.setItemId(request.getItemId());
        periodRequest.setItemName(request.getItemName());
        itemPeriodStageRepository.saveProductStage(periodRequest);
        return new ItemPeriodStageResponse();
    }

    /**
     * 修改商品状态
     *
     * @param request itemId、itemName、itemStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemPeriodStageResponse updateItemStatus(ItemPeriodStatusRequest request) {
        // 入参检查
        checkUpdateParams(request);
        // 修改
        return itemPeriodStageRepository.updateItemStatus(request);
    }

    /**
     * 查询商品的状态
     *
     * @param itemId itemId
     * @return 商品状态，1-准备上架，2-试销上架，3-上架，4-预下架，5-下架，6-停售
     */
    @Override
    public Integer getItemStatus(String itemId) {
        // 参数检查
        if (StringUtils.isEmpty(itemId)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
        // 查询
        return itemPeriodStageRepository.getItemStatus(itemId);
    }

    /**
     * 检查参数
     *
     * @param request
     */
    private void checkParams(ItemPeriodStatusRequest request) {
        if (Objects.isNull(request) || StringUtils.isEmpty(request.getItemId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
    }

    /**
     * 检查参数，更新操作
     *
     * @param request
     */
    private void checkUpdateParams(ItemPeriodStatusRequest request) {
        checkParams(request);

        if (Objects.isNull(request.getItemStatus())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
    }
}
